package com.shoujifeng.snowmusic.player;

import com.shoujifeng.snowmusic.entity.AnimaAccess;
import com.shoujifeng.snowmusic.player.R;
import com.shoujifeng.snowmusic.player.http.ServerAccess;

import android.R.integer;
import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.KeyListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TextView.OnEditorActionListener;

public class OnlineTab extends TabActivity {
	private Button firstBtn;
	private Button secondBtn;
	private Button thirdBtn;
	private Button fourthBtn;
	private Button fifthBtn;
	private Button lastButton;
	private Intent mIntent1;
	private Intent mIntent2;
	private Intent mIntent3;
	private Intent mIntent4;
	private Intent mIntent5;
	private Intent mIntentNO;
	private final int TAB_1		= 0;
	private final int TAB_2		= 1;
	private final int TAB_3		= 2;
	private final int TAB_4		= 3;
	private final int TAB_5		= 4;
	private final int TAB_NO		= 5;
	private int LAST_TAB    = -1;
	private TabHost mTabHost;
	private View mTmpView;
	private EditText onlineSearchEdt;
	private Button searchbuttonclear;
	
	private AnimaAccess mAnimaAccess;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.online_tab);
		init();
		
		mAnimaAccess = new AnimaAccess(this);
	}

	private void init(){
		mTabHost = getTabHost();
		firstBtn = (Button) findViewById(R.id.firstBtn);
		secondBtn = (Button) findViewById(R.id.secondBtn);
		thirdBtn = (Button) findViewById(R.id.thirdBtn);
		fourthBtn = (Button) findViewById(R.id.fourthBtn);
		fifthBtn = (Button) findViewById(R.id.fifthBtn);
		
		firstBtn.setOnClickListener(onClickListener);
		secondBtn.setOnClickListener(onClickListener);
		thirdBtn.setOnClickListener(onClickListener);
		fourthBtn.setOnClickListener(onClickListener);
		fifthBtn.setOnClickListener(onClickListener);
		
		lastButton = firstBtn;
		firstBtn.setTextColor(getResources().getColor(R.color.white));
		
		mTmpView = firstBtn;
		
		mIntent1 = new Intent(this, OnLineMusicActivity.class);
		mIntent2 = new Intent(this, OnlineHotActivity.class);
		mIntent3 = new Intent(this, OnlineSpecialActivity.class);
		mIntent4 = new Intent(this, OnlinePlayerActivity.class);
		mIntent5 = new Intent(this, OnlineOrderActivity.class);
		mIntentNO = new Intent(this, NotNetActivity.class);
		
		mIntent1.putExtra("mIntent1", true);
		mIntent2.putExtra("mIntent2", true);
		mIntent3.putExtra("mIntent3", true);
		mIntent4.putExtra("mIntent4", true);
		mIntent5.putExtra("mIntent5", true);
		mIntent5.putExtra("mIntentNO", true);
		
		setTabIndicator("", 1, mIntent1);
		setTabIndicator("", 2, mIntent2);
		setTabIndicator("", 3, mIntent3);
		setTabIndicator("", 4, mIntent4);
		setTabIndicator("", 5, mIntent5);
		setTabIndicator("", 6, mIntentNO);
		
		 checkTab(); 
		 
		searchbuttonclear = (Button) findViewById(R.id.searchbuttonclear);
		searchbuttonclear.setOnClickListener(onClickListener);
		onlineSearchEdt = (EditText) findViewById(R.id.onlineSearchEdt);
		
		onlineSearchEdt.setOnEditorActionListener(new OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				
				if(actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
					
					((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE))
					.hideSoftInputFromWindow(
							OnlineTab.this.getCurrentFocus().getWindowToken(), 
							InputMethodManager.HIDE_NOT_ALWAYS);   
					
					Intent intent = new Intent(OnlineTab.this,OnlineSearch.class);
					intent.putExtra("content", onlineSearchEdt.getText().toString());
					startActivity(intent);
					
				}
				
				return false;
			}
		});
	}
	
	
	@Override
	protected void onResume() {
		
		if(LAST_TAB == TAB_NO) {
			if(!checkTab()) {
				mTabHost.setCurrentTab(TAB_1);
				firstBtn.setBackgroundResource(R.drawable.online_menu_bg);
				secondBtn.setBackgroundResource(R.drawable.onlinebtnbg);
				thirdBtn.setBackgroundResource(R.drawable.onlinebtnbg);
				fourthBtn.setBackgroundResource(R.drawable.onlinebtnbg);
				fifthBtn.setBackgroundResource(R.drawable.onlinebtnbg);
				
				LAST_TAB = TAB_1;
			}
		} else {
			checkTab();
		}
		
		super.onResume();
	}

	private boolean checkTab() {
		SharedPreferences spf = PreferenceManager.getDefaultSharedPreferences(OnlineTab.this);
		   String value = spf.getString(ServerAccess.SET_WIFI, "no");   //参数: 1-要读出的键值  2- 默认值(就是读取失败后返回的值)
		   boolean setWifi = ServerAccess.checkNetworkConnection(OnlineTab.this);
		if(!setWifi&&value.equals("yes")){
			mTabHost.setCurrentTab(TAB_NO);
			
			LAST_TAB = TAB_NO;
			return true;
		}
		
		return false;
	}
		
private View.OnClickListener onClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			 if(checkTab())
				 return;
			 
			 if(R.id.searchbuttonclear == v.getId()) {
				 ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE))
					.hideSoftInputFromWindow(
							OnlineTab.this.getCurrentFocus().getWindowToken(), 
							InputMethodManager.HIDE_NOT_ALWAYS);   
					
					Intent intent = new Intent(OnlineTab.this,OnlineSearch.class);
					intent.putExtra("content", onlineSearchEdt.getText().toString());
					startActivity(intent);
					
					return;
			 }
			
			if(mTmpView == v)
				return;
			
			int animTime = 300;
			
			switch (v.getId()) {
			
			case R.id.firstBtn:
			{
				mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(0f, 1.0f, 0f, 0f, animTime, false));
				mTabHost.setCurrentTab(TAB_1);
				mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(-1.0f, 0f, 0f, 0f, animTime, false));
				
					firstBtn.setBackgroundResource(R.drawable.online_menu_bg);
					secondBtn.setBackgroundResource(R.drawable.onlinebtnbg);
					thirdBtn.setBackgroundResource(R.drawable.onlinebtnbg);
					fourthBtn.setBackgroundResource(R.drawable.onlinebtnbg);
					fifthBtn.setBackgroundResource(R.drawable.onlinebtnbg);
					
					LAST_TAB = TAB_1;
					
					lastButton.setTextColor(getResources().getColor(R.color.black));
					firstBtn.setTextColor(getResources().getColor(R.color.white));
					lastButton = firstBtn;
			}
			break;
			case R.id.secondBtn:
			{
				if(LAST_TAB > TAB_2) {
					mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(0f, 1.0f, 0f, 0f, animTime, false));
					mTabHost.setCurrentTab(TAB_2);
					mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(-1.0f, 0f, 0f, 0f, animTime, false));
					
				}
				else {
					mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(0f, -1.0f, 0f, 0f, animTime, false));
					mTabHost.setCurrentTab(TAB_2);
					mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(1.0f, 0f, 0f, 0f, animTime, false));
					
				}
				firstBtn.setBackgroundResource(R.drawable.onlinebtnbg);
				secondBtn.setBackgroundResource(R.drawable.online_menu_bg);
				thirdBtn.setBackgroundResource(R.drawable.onlinebtnbg);
				fourthBtn.setBackgroundResource(R.drawable.onlinebtnbg);
				fifthBtn.setBackgroundResource(R.drawable.onlinebtnbg);
				
				LAST_TAB = TAB_2;
				
				lastButton.setTextColor(getResources().getColor(R.color.black));
				secondBtn.setTextColor(getResources().getColor(R.color.white));
				lastButton = secondBtn;
			}
			break;
			case R.id.thirdBtn:
			{
				if(LAST_TAB > TAB_3) {
					mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(0f, 1.0f, 0f, 0f, animTime, false));
					mTabHost.setCurrentTab(TAB_3);
					mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(-1.0f, 0f, 0f, 0f, animTime, false));
					
				}
				else {
					mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(0f, -1.0f, 0f, 0f, animTime, false));
					mTabHost.setCurrentTab(TAB_3);
					mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(1.0f, 0f, 0f, 0f, animTime, false));
					
				}
				
				firstBtn.setBackgroundResource(R.drawable.onlinebtnbg);
				secondBtn.setBackgroundResource(R.drawable.onlinebtnbg);
				thirdBtn.setBackgroundResource(R.drawable.online_menu_bg);
				fourthBtn.setBackgroundResource(R.drawable.onlinebtnbg);
				fifthBtn.setBackgroundResource(R.drawable.onlinebtnbg);
				
				LAST_TAB = TAB_3;
				
				lastButton.setTextColor(getResources().getColor(R.color.black));
				thirdBtn.setTextColor(getResources().getColor(R.color.white));
				lastButton = thirdBtn;
			}
			break;
			case R.id.fourthBtn:
			{
				if(LAST_TAB > TAB_4) {
					mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(0f, 1.0f, 0f, 0f, animTime, false));
					mTabHost.setCurrentTab(TAB_4);
					mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(-1.0f, 0f, 0f, 0f, animTime, false));
					
				}
				else {
					mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(0f, -1.0f, 0f, 0f, animTime, false));
					mTabHost.setCurrentTab(TAB_4);
					mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(1.0f, 0f, 0f, 0f, animTime, false));
					
				}
				
				firstBtn.setBackgroundResource(R.drawable.onlinebtnbg);
				secondBtn.setBackgroundResource(R.drawable.onlinebtnbg);
				thirdBtn.setBackgroundResource(R.drawable.onlinebtnbg);
				fourthBtn.setBackgroundResource(R.drawable.online_menu_bg);
				fifthBtn.setBackgroundResource(R.drawable.onlinebtnbg);
				
				LAST_TAB = TAB_4;
				
				lastButton.setTextColor(getResources().getColor(R.color.black));
				fourthBtn.setTextColor(getResources().getColor(R.color.white));
				lastButton = fourthBtn;
			}
			break;
			case R.id.fifthBtn:
			{
				mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(0f, -1.0f, 0f, 0f, animTime, false));
				mTabHost.setCurrentTab(TAB_5);
				mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(1.0f, 0f, 0f, 0f, animTime, false));
				
				firstBtn.setBackgroundResource(R.drawable.onlinebtnbg);
				secondBtn.setBackgroundResource(R.drawable.onlinebtnbg);
				thirdBtn.setBackgroundResource(R.drawable.onlinebtnbg);
				fourthBtn.setBackgroundResource(R.drawable.onlinebtnbg);
				fifthBtn.setBackgroundResource(R.drawable.online_menu_bg);
				
				LAST_TAB = TAB_5;
				
				lastButton.setTextColor(getResources().getColor(R.color.black));
				fifthBtn.setTextColor(getResources().getColor(R.color.white));
				lastButton = fifthBtn;
			}
			break;
			default:
				break;
			}
			
			mTmpView = v;
			
		}
	};
	
	private void setTabIndicator(String titleString, int nId, Intent intent)
	{
		// 使用指定样式的TAB
		View view = LayoutInflater.from(this.mTabHost.getContext()).inflate(
				R.layout.tab_style, null);
		String idString = String.valueOf(nId);
		// 创建一个新Tab
		TabHost.TabSpec localTabSpec = mTabHost.newTabSpec(idString)
				.setIndicator(view).setContent(intent);
		mTabHost.addTab(localTabSpec);
	}
}
